<template>
	<scroll-view style=" height: 100vh" @scrolltolower="lowerBottom" scroll-y="true" refresher-enabled="true"
		:refresher-triggered="triggered" @refresherrefresh="onRestore">
		<van-search shape="round" placeholder="请输入搜索关键词" background="rgba(251, 251, 251, 0.859);
" use-action-slot @change="onChange" class="searchBox" @focus='this.ifshowSecelct = true' v-model="msg">
			<!-- <button class="cu-btn round bg-white shadow" slot="action" @tap="onClick">搜索</button> -->
			<van-button style="padding: 3px;" icon="guide-o" type="info" slot="action" @tap="onClick">搜索</van-button>

		</van-search>
		<van-tree-select :items="items" :main-active-index="mainActiveIndex"
			:active-id.sync="mainActiveIndex == 0 ? time : (mainActiveIndex == 1 ? day : place)"
			@click-nav="onClickTreeNav" @click-item="onClickTreeItem" height='150px' v-if="ifshowSecelct" />
		<view style="width: 100vw;height: 2vh;">

		</view>
		<van-empty description="暂无相关" v-if="mesList.length == 0" />
		<view v-for="(item, index) in mesList" :key="index" style="position: relative;" v-else>
			<message :content='item.content' :posterid='item.posterid' :type='item.type' :title='item.title'
				:time='item.time' :avatar='item.avatar'></message>
		</view>
	</scroll-view>
</template>

<script>
	import {
		showAllpost,
		getTags
	} from '@/store/api.js'
	import {
		truncateString,
	} from '@/store/constants.js'
	import message from '@/modul/Search/messagelist.vue'
	export default {
		components: {
			message
		},
		mounted() {
			getTags().then(res => {
				console.log(res)
				console.log('看这里')
				console.log(this.items[2].children)
				res.data.data.forEach(item => {
					let temp = {
						text: item.name,
						id: item.id
					}
					this.items[2].children.push(temp)
				})
			}).catch(err => {
				console.log(err)
			})
			uni.$on('showsearch', (value) => {
				this.ifshowSecelct = value;
			});
			let data = {
				pageNum: 1,
				pageSize: 20,
				tagId: '',
				q: ''
			}
			showAllpost(data).then(res => {
				this.total = res.data.data.total
				// console.log(res)
				this.nowdata.pageNum++
				res.data.data.rows.forEach(item => {
					this.mesList.push({
						posterid: item.id,
						avatar: item.posterAvatar,
						type: JSON.stringify(item.tags),
						content: item.content,
						title: item.title,
						time: item.beginTime
					})
				})
			})
		},
		data() {
			return {
				triggered: false,
				total: 0,
				nowdata: {
					pageNum: 1,
					pageSize: 20,
				},
				page: 1,
				values: '',
				mainActiveIndex: 0,
				time: '',
				place: '',
				day: '',
				mesList: [],
				ifshowSecelct: false,
				max: 2,
				msg: '',
				items: [{
						text: '时间',
						children: [{
								text: '00:00-04:00',
								id: 13
							},
							{
								text: '04:00-08:00',
								id: 14
							},
							{
								text: '08:00-12:00',
								id: 15
							},
							{
								text: '12:00-16:00',
								id: 16
							},
							{
								text: '16:00-20:00',
								id: 17
							},
							{
								text: '20:00-0:00',
								id: 18
							}
						]
					},
					{
						text: '日期',
						children: [{
								text: (new Date().getFullYear()) + '-' + ((new Date().getMonth() + 1) > 9 ?
										(
											new Date().getMonth() + 1) : `0${(new Date().getMonth() + 1)}`) +
									'-' + (
										new Date().getDate() > 9 ? new Date().getDate() :
										`0${new Date().getDate()}`),
								id: 19
							},
							{
								text: (new Date().getFullYear()) + '-' + ((new Date().getMonth() + 1) > 9 ?
										(
											new Date().getMonth() + 1) : `0${(new Date().getMonth() + 1)}`) +
									'-' + (
										new Date().getDate() + 1 > 9 ? new Date().getDate() + 1 :
										`0${new Date().getDate() + 1}`),
								id: 20
							},
							{
								text: (new Date().getFullYear()) + '-' + ((new Date().getMonth() + 1) > 9 ?
										(
											new Date().getMonth() + 1) : `0${(new Date().getMonth() + 1)}`) +
									'-' + (
										new Date().getDate() + 2 > 9 ? new Date().getDate() + 2 :
										`0${new Date().getDate() + 2}`),
								id: 21
							},
							{
								text: (new Date().getFullYear()) + '-' + ((new Date().getMonth() + 1) > 9 ?
										(
											new Date().getMonth() + 1) : `0${(new Date().getMonth() + 1)}`) +
									'-' + (
										new Date().getDate() + 3 > 9 ? new Date().getDate() + 3 :
										`0${new Date().getDate() + 3}`),
								id: 22
							},
							{
								text: (new Date().getFullYear()) + '-' + ((new Date().getMonth() + 1) > 9 ?
										(
											new Date().getMonth() + 1) : `0${(new Date().getMonth() + 1)}`) +
									'-' + (
										new Date().getDate() + 4 > 9 ? new Date().getDate() + 4 :
										`0${new Date().getDate() + 4}`),
								id: 23
							},
							{
								text: (new Date().getFullYear()) + '-' + ((new Date().getMonth() + 1) > 9 ?
										(
											new Date().getMonth() + 1) : `0${(new Date().getMonth() + 1)}`) +
									'-' + (
										new Date().getDate() + 5 > 9 ? new Date().getDate() + 5 :
										`0${new Date().getDate() + 5}`),
								id: 24
							}
						]
					},
					{
						text: '活动类型',
						children: []
					}
				]
			};
		},
		methods: {
			onRestore() {
				console.log('onRestore')
				this.triggered=true
				this.total= 0
				this.nowdata= {
					pageNum: 1,
					pageSize: 20
				}
				this.page= 1
				this.values= ''
				this.mainActiveIndex= 0
				this.time= ''
				this.place= ''
				this.day= ''
				this.mesList= []
				this.ifshowSecelct= false
				this.max= 2
				this.msg= ''
				getTags().then(res => {
					console.log(res)
					console.log('看这里')
					console.log(this.items[2].children)
					res.data.data.forEach(item => {
						let temp = {
							text: item.name,
							id: item.id
						}
						this.items[2].children.push(temp)
					})
				}).catch(err => {
					console.log(err)
				})
				let data = {
					pageNum: 1,
					pageSize: 20,
					tagId: '',
					q: ''
				}
				showAllpost(data).then(res => {
					this.total = res.data.data.total
					// console.log(res)
					res.data.data.rows.forEach(item => {
						this.mesList.push({
							posterid: item.id,
							avatar: item.posterAvatar,
							type: JSON.stringify(item.tags),
							content: item.content,
							title: item.title,
							time: item.beginTime
						})
					})
				})
				setTimeout(() => {
					console.log('刷新完成')
					this.triggered = false
				}, 1000)
			},
			Sear(e) {
				e.detail = ''
			},
			onClick() {
				this.ifshowSecelct = false
				uni.showLoading({
					title: '搜索中'
				})
				let data = {}
				if (this.day != '') {
					let D = this.items[1].children[Number(this.day) - 19].text
					if (this.time == '') {
						data = {
							pageNum: 1,
							pageSize: 20,
							tagId: this.place,
							q: this.msg,
							beginTime: D + 'T' + '00:00:00.000+00:00',
							endTime: D + 'T' + '23:59:59.000+00:00',
						}
					} else {
						let T = this.items[0].children[Number(this.time) - 13].text
						data = {
							pageNum: 1,
							pageSize: 20,
							tagId: this.place,
							q: this.msg,
							beginTime: D + 'T' + T.substring(0, 2) + ':00:00.000+00:00',
							endTime: D + 'T' + T.substring(6, 8) + ':00:00.000+00:00'
						}
					}
				} else {
					data = {
						pageNum: 1,
						pageSize: 20,
						tagId: this.place,
						q: this.msg
					}
				}
				console.log(data)
				showAllpost(data).then(res => {
					this.total = res.data.data.total
					this.nowdata = data
					this.nowdata.pageNum++
					this.mesList = []
					res.data.data.rows.forEach(item => {
						this.mesList.push({
							posterid: item.id,
							avatar: item.posterAvatar,
							type: JSON.stringify(item.tags),
							content: truncateString(item.content),
							title: item.title,
							time: item.beginTime
						})
					})
					uni.hideLoading()
				})
				this.day = ''
				this.place = ''
				this.time = ''
			},
			lowerBottom() {
				if (this.total > this.mesList.length) {
					let data = this.nowdata
					console.log(data)
					showAllpost(data).then(res => {
						console.log(res.data.data.rows)
						this.nowdata.pageNum++
						res.data.data.rows.forEach(item => {
							this.mesList.push({
								posterid: item.id,
								avatar: item.posterAvatar,
								type: JSON.stringify(item.tags),
								content: truncateString(item.content),
								title: item.title,
								time: item.beginTime
							})
						})
					})
				}else{
					uni.showToast({
						duration:1000,
						icon:'none',
						title:'没有更多了'
					})
				}

			},
			onChange(e) {
				// console.log(e.detail)
				this.msg = e.detail
			},
			onClickTreeNav(event) {
				this.mainActiveIndex = event.detail.index || 0;
			},
			onClickTreeItem(event) {
				const detail = event.detail || {};
				if (detail.id >= 13 && detail.id < 19)
					this.time = detail.id
				else if (detail.id < 13) {
					this.place = detail.id
				} else {
					this.day = detail.id
				}
			}

		}
	};
</script>

<style scoped>
	.box {
		margin-bottom: 30rpx;
	}

	.select {
		background-color: rgba(251, 251, 251, 0.859) !important;

	}
</style>